package Tree;

public class _654_MaximumBinaryTree {
    private int[] nums;
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        this.nums = nums;
        if (nums.length == 0) {
            return null;
        }
        return buildTree(0, nums.length-1);
    }

    public TreeNode buildTree(int start, int end) {
        if (start > end) {
            return null;
        }
        int max = getMax(start, end);
        TreeNode temp = new TreeNode(nums[max]);
        temp.left = buildTree(start, max - 1);
        temp.right = buildTree(max + 1, end);
        return temp;
    }

    public int getMax(int begin, int end) {
        int max = begin;
        for (int i = begin; i <= end; i++) {
            if (nums[i] > nums[max]) {
                max = i;
            }
        }
        return max;
    }
}
